import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import cesium from 'vite-plugin-cesium'

// https://vitejs.dev/config/
export default defineConfig({
  base: '/seabed2',
  plugins: [vue(), vueJsx(), cesium()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: '0.0.0.0',
    port: 8000,
    open: false,
    proxy: {
      '/api': {
        target: 'http://47.94.129.233:8849',
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp('^/api'), ''),
        secure: false,
        configure: (proxy, options) => {
          // 配置此项可在响应头中看到请求的真实地址
          proxy.on('proxyRes', (proxyRes, req) => {
            proxyRes.headers['x-real-url'] = new URL(req.url || '', options.target)?.href || ''
          })
        }
      }
    }
  }
})
